'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0112
    Inherits Snac

    Private _tlvsSize As Integer

    Public Sub New()
        MyBase.New(&H1, &H12)
    End Sub

    Private _Families As New List(Of Integer)
    Public ReadOnly Property Families() As List(Of Integer)
        Get
            Return _Families
        End Get
    End Property


    Private _ServerAddress As New TlvServerAddress
    Public ReadOnly Property ServerAddress() As TlvServerAddress
        Get
            Return _ServerAddress
        End Get
    End Property

    Private _AuthorizationCookie As New TlvAuthorizationCookie
    Public ReadOnly Property AuthorizationCookie() As TlvAuthorizationCookie
        Get
            Return _AuthorizationCookie
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Dim familyCount As Integer = ByteConverter.ToUInt16(data.GetRange(index, 2))
        Dim familyIndex As Integer

        index += 2

        Do While familyIndex < familyCount
            _Families.Add(ByteConverter.ToUInt16(data.GetRange(index, 2)))

            index += 2
        Loop

        Do While index + 4 <= data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H5
                    _ServerAddress.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H6
                    _ServerAddress.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            _tlvsSize += desc.TotalSize
            index += desc.TotalSize
        Loop

        SetTotalSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + 2 * _Families.Count + _tlvsSize
    End Function
End Class
